const App = getApp();
// 默认未登录的灰色头像
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
Page({
  data: {
    // 页面标题
    type:0,
    trueMobile:'',
    wzList:[],
    detail:{},
    hallShareTitle:'选择你喜欢的Ta，为Ta投上一票吧',  //_this.data.page.params.share_title
    isLoading: true, // 是否正在加载中
  },
  onLoad: function(options) {
    let _this = this;
    this.data.type = options.type>0 ? options.type : 0;
    // 设置navbar标题、颜色
    App.setNavigationBar();
    
    App._get('article/getTypeObj', {category_id: _this.data.type}, function(result) {
      let _detail = result.data.detail;
      _this.setData({detail:_detail});
    });
  },
  onShow(){
    App.pageOnLoad(this);
    this.setData({trueMobile:wx.getStorageSync('true_mobile')});
    this.getArticleList();
  },
  getArticleList(isPage, page) {
    let _this = this;
    App._get('article/lists', { page: page || 1, category_id: _this.data.type}, function(result) {
      let resList = result.data.list,
        dataList = _this.data.articleList;
      if (isPage == true) {
        _this.setData({ wzList: dataList.data.concat(resList.data), isLoading: false, });
      } else {
        _this.setData({ wzList: resList.data, isLoading: false, });
      }
    });
  },
  //跳转到文章详情
  onTargetDetail(e) {
    wx.navigateTo({
      url: '/pages/article/detail/index?toupiao=1&article_id=' + e.currentTarget.dataset.id
    });
  },
  //获取电话号码之后
  getUserMobile22(e){
    const _this = this;
    var wzid = e.currentTarget.dataset.wzid
    App.getUserMobile(e, function(mobile, myPara){
      _this.setData({trueMobile: mobile});
      _this.toupiaoFun(myPara);
    }, wzid);
  },
  //确定开始投票函数
  toupiaoClick(e){
    var wzid = e.currentTarget.dataset.wzid
    this.toupiaoFun(wzid);
  },
  toupiaoFun(wzid){
    //App.showError('开始投票'+wzid);
    const _this = this;
    var _oldList = this.data.wzList;
    App._get('article/addtoupiao', {tpartid:wzid, userid:wx.getStorageSync('user_id')}, function(result) {
      let _back = result.data.back;
      if(_back.isok=='no'){
        App.showError(_back.msg);
      }
      else{
        for(var i=0,cnt=_oldList.length;i<cnt;i++){
          if(_oldList[i]['article_id']==wzid){
            _oldList[i]['get_piaonum'] = _back['piaoNum'];
            break;
          }
        }
        _this.setData({wzList:_oldList});
        App.showError('投票成功');
      }
    });
  },
  onShareAppMessage: function() {
    const _this = this;
    const params = App.getShareUrlParams({ 'type': _this.data.type });
    return {
      title: _this.data.hallShareTitle,
      path: "/pages/toupiao/index?" + params
    };
  },
  onShareTimeline: function() {
    const _this = this;
    const params = App.getShareUrlParams({ 'type': _this.data.type });
    return {
      title: _this.data.hallShareTitle,
      path: "/pages/toupiao/index?" + params
    };
  }
});